﻿using System.ComponentModel;

namespace ElevatorDispatching
{
    public class ElevatorInfoDataSource : BindableBase
    {
        private Elevator _elevator;
        public ElevatorInfoDataSource(Elevator elevator)
        {
            _elevator = elevator;
            _elevator.PropertyChanged += ElevatorStatusChanged;
            _elevator.DoorState.PropertyChanged += ElevatorStatusChanged;
            RefreshInfoText();
        }

        private void ElevatorStatusChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case nameof(Elevator.CurrentFloor):
                case nameof(Elevator.RunningDirection):
                case nameof(Elevator.RunningStatus):
                case nameof(DoorState.IsOpen):
                    RefreshInfoText();
                    break;
                default:
                    break;
            }
        }

        private void RefreshInnerInfoText()
        {
            var infoText = $"{_elevator.CurrentFloor.Id}";
            switch (_elevator.RunningDirection)
            {
                case Direction.Up:
                    infoText += $" ↑";
                    break;
                case Direction.Down:
                    infoText += $" ↓";
                    break;
            }
            if (_elevator.DoorState.IsOpen) infoText = $"> {infoText} <";
            InnerInfoText = infoText;
        }

        private void RefreshOuterInfoText()
        {
            var infoText = $"{_elevator.CurrentFloor.Id}";
            switch (_elevator.RunningDirection)
            {
                case Direction.Up:
                    infoText += $" ↑";
                    break;
                case Direction.Down:
                    infoText += $" ↓";
                    break;
            }
            switch (_elevator.RunningStatus)
            {
                case RunningStatus.Running:
                    infoText += "(R)";
                    break;
            }
            if (_elevator.DoorState.IsOpen) infoText += "(O)";
            OuterInfoText = infoText;
        }

        private void RefreshInfoText()
        {
            RefreshInnerInfoText();
            RefreshOuterInfoText();
        }

        private string _innerInfoText;

        public string InnerInfoText { get => _innerInfoText; private set => SetProperty(ref _innerInfoText, value); }


        private string _outerInfoText;

        public string OuterInfoText { get => _outerInfoText; private set => SetProperty(ref _outerInfoText, value); }

    }
}
